package com.chain.voter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.chain.voter.modle.Candidate;
import com.chain.voter.record.FavouriteShare;
import com.naschain.naspay.NasPayManager;
import com.naschain.naspay.nebulas.utils.Util;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by baoyunfeng on 2018/6/23.
 */

public class ProgressView extends LinearLayout {

    @BindView(R.id.progressBar)
    ProgressBar progressBar;

    @BindView(R.id.votenumber)
    TextView votenumber;

    @BindView(R.id.vote)
    View vote;

    public ProgressView(Context context) {
        this(context, null);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews();
    }

    private void initViews() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.view_progress, this, true);
        ButterKnife.bind(this, rootView);
    }

    public void setProcess(final String key, final Candidate candidate, int max,final boolean votable,final String name) {
        progressBar.setMax(max);
        progressBar.setProgress(candidate.getValue().getNumber());
        votenumber.setText(candidate.getValue().getName() + "：" + candidate.getValue().getNumber() + "票");
        if (votable) {
            vote.setVisibility(VISIBLE);
            vote.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    String randomCode = Util.getRandomCode(32);
                    NasPayManager.createCallBuilder(getContext())
                            .setFunctionName("update")
                            .setArgs(new String[]{key, candidate.getKey()})
                            .setSerialNumber(randomCode)
                            .build();
                    Toast.makeText(getContext(),"投票正在进行，请稍后再刷新数据",Toast.LENGTH_SHORT).show();

                    FavouriteShare.addPendingPayRecord(getContext(),randomCode,"投票问题："+name);
                }
            });
        } else {
            vote.setVisibility(GONE);
    }


    }

}
